import { http } from './http'
import { system } from './url'

export function login(params: HttpBkhc.Login) {
    const url = system.login
    return http.get<HttpBkhc.User>(url, params)
}

export function getRole() {
    const url = system.getRole
    return (http.post(url, {}).then(res => {
        if (res.code === 200) {
            if (res.data && res.data.roles instanceof Array && res.data.roles.length > 0) {
                res.data = res.data.roles[0]
            } else {
                res.data = {
                    role_code: '',
                    role_name: ''
                }
            }
        }
        return res
    })) as Promise<HttpResponseData<HttpBkhc.Role>>
}

export function register(data: HttpBkhc.RegisterInfo) {
    const url = system.register
    const sendData = {
        fullName: data.idCardName,
        password: data.password,
        phone: data.phone,
        userType: data.userType,
        username: data.username,
        inviteCode: data.invitationCode,
        applyInfomation: {}
    }
    if (data.userType === 1) {
        sendData.applyInfomation = {
            businessType: data.identity,
            card: data.idCardNo,
            cardBack: data.idCardPicBack,
            cardFront: data.idCardPicFront,
            licensePath: data.businessLicensePic
        }
    }
    return http.post<string>(url, sendData)
}

export function getLookup(name:string){
    const url = system.getLookup;
    return http.get(url, {classItem:name})
}
export function getMember(){
    const url = system.getMember;
    return http.post(url)
}